package com.leo;

import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @ClassName AnimationDemo
 * @Description 跑马灯效果
 * @Author wangss
 * @date 2020.10.27 20:09
 * @Version 1.0
 */
public class AnimationDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AtomicReference<Double> x = new AtomicReference<>((double) 0);

        Button button1 = new Button("上一张");
        Button button2 = new Button("下一张");

        Pane pane = new Pane();
        TranslateTransition tt = new TranslateTransition();
        tt.setNode(pane);
        tt.setInterpolator(Interpolator.LINEAR);

        StackPane sp = new StackPane();
        sp.setPrefWidth(400);
        sp.setPrefHeight(400);
        sp.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, null, new BorderWidths(2))));

        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.getChildren().add(new Button("button1"));
        hBox.setPrefWidth(400);
        hBox.setPrefHeight(400);
        hBox.setBackground(new Background(new BackgroundFill(Color.CORNFLOWERBLUE, null, null)));

        HBox hBox2 = new HBox();
        hBox2.setAlignment(Pos.CENTER);
        hBox2.getChildren().add(new Button("button2"));
        hBox2.setPrefWidth(400);
        hBox2.setPrefHeight(400);
        hBox2.setBackground(new Background(new BackgroundFill(Color.BLANCHEDALMOND, null, null)));
        hBox2.setBorder(new Border(new BorderStroke(Color.AQUA, BorderStrokeStyle.SOLID, null, new BorderWidths(2))));

        sp.getChildren().addAll(hBox, hBox2);

        DisplacementMap displacementMap = new DisplacementMap();
        hBox2.setEffect(displacementMap);

        DisplacementMap displacementMap2 = new DisplacementMap();
        hBox.setEffect(displacementMap2);
        displacementMap2.setOffsetX(0);

        pane.translateXProperty().addListener((observable, oldValue, newValue) -> {
            x.set(newValue.doubleValue());
            displacementMap.setOffsetX(-(newValue.doubleValue() / 400));
            displacementMap2.setOffsetX(1 - (newValue.doubleValue() / 400));
        });

        tt.setOnFinished(event -> {
            System.out.println(displacementMap.getOffsetX());
            System.out.println(displacementMap2.getOffsetX());
            System.out.println(x.get());
            System.out.println(tt.getDuration());
        });

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(button1, button2, sp);

        AnchorPane.setTopAnchor(sp, 200d);
        AnchorPane.setLeftAnchor(sp, 500d);
        AnchorPane.setTopAnchor(button1, 620d);
        AnchorPane.setLeftAnchor(button1, 630d);
        AnchorPane.setTopAnchor(button2, 620d);
        AnchorPane.setLeftAnchor(button2, 710d);

        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(1500);
        primaryStage.setHeight(900);
        primaryStage.centerOnScreen();
        primaryStage.setTitle("跑马灯效果展示");
        primaryStage.show();

        button1.setOnAction(event -> {
            if (tt.getStatus().name().equals(Animation.Status.RUNNING.name())) {
                tt.stop();
            }

            if (x.get() != 0) {
                tt.setFromX(x.get());
                tt.setToX(0);
                tt.setDuration(Duration.seconds((x.get()/400)*2));
                tt.play();
            }
        });

        button2.setOnMouseClicked(event -> {
            if (tt.getStatus().name().equals(Animation.Status.RUNNING.name())) {
                tt.stop();
            }
            if (x.get() != 400) {
                tt.setDuration(Duration.seconds(((400 - x.get())/400)*2));
                tt.setFromX(x.get());
                tt.setToX(400);
                tt.play();
            }
        });
    }
}
